/*
 * ===================================================
 *
 *       Filename:  exec.c
 *
 *
 *        Created:  10/08/2011 05:32:59 PM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  durand_y, <durand.yoan@gmail.com>
 *
 * ===================================================
 */
#include "find.h"
static s_list   *fill_dirlist(int   *i,
                              int   *argc,
                              char  **argv)
{
    s_list      *dl = NULL;

    dl = init();
    if (argv[(*i)][0] == '-')
        dl = add(dl, ".");
    else
    {
        for (; (((*i) < *argc) && (argv[(*i)][0] != '-')); (*i)++)
        {
            dl = add(dl, argv[(*i)]);
        }
      }
    return (dl);
}

static void   add_opt(char        **argv,
                      int         *i,
                      int         *argc,
                      s_list      **list)
{
    if((mystrcmp(argv[(*i)], "-name") == 1) ||
               (mystrcmp(argv[(*i)], "-newer") == 1) ||
               (mystrcmp(argv[(*i)], "-atime") == 1))
            {
                (*list) = add((*list), argv[(*i)]);
                if ((*i + 1) < (*argc))
                {
                    (*list) = add((*list), argv[(*i + 1)]);
                }
                else
                {
                    my_error2("./find", argv[(*i)], 1);
                    exit (1);
                }
                (*i) += 2;
            }
            else
                if ((mystrcmp(argv[(*i)], "-help") == 1) ||
                    (mystrcmp(argv[(*i)], "--help") == 1))
                {
                    (*list) = add((*list), argv[(*i)]);
                    (*i)++;
                }
                else
                    my_error(argv[0], argv[(*i)]);

}

static void     fill_op(int        *argc,
                        int        *i,
                        char       **argv,
                        s_list     *list_op)
{
    while ((*i) < (*argc))
    {
        if (argv[(*i)][0] == '-')
        {
            add_opt(argv, i, argc, &list_op);
        }
        else
        {
               my_error2("./find", argv[(*i)], 2);
               usage("./find");
               exit(1);
        }
    }
}

int      test_next(s_list    *l)
{
    if (l->next->next == NULL)
        return (1);
    else
        return (0);
}

static int      exec(s_list         *list_op,
                     s_list         *list)
{
    while (list_op != NULL)
    {
        if ((mystrcmp(list_op->dir, "--help") == 1) ||
           (mystrcmp(list_op->dir, "-help") == 1))
            exec_help("./find");
        if (mystrcmp(list_op->dir, "-name") == 1)
        {
            list_op = exec_name(list_op, &list);
            continue;
        }
        if (mystrcmp(list_op->dir, "-newer") == 1)
        {
            list_op = exec_newer(list_op, &list);
            continue;
        }
        if (mystrcmp(list_op->dir, "-atime") == 1)
        {
            list_op = exec_atime(list_op, &list);
            continue;
        }
        else
            exec_error("./find", list_op->dir);
    }
    return (0);
}

int      exec1(int           *i,
               char          **argv,
               int           *argc,
               s_list        *list,
               s_list        *dirlist)
{
    s_list      *list_op = NULL;

    list_op = init();
    parse_short(argc, i, argv);
     dirlist = fill_dirlist(i, argc, argv);
    fill_op(argc, i, argv, list_op);
     while(dirlist != NULL)
     {
         listdir(dirlist->dir, list);
         dirlist = dirlist->next;
     }
            if (list_op->size > 0)
            {
                    return (exec (list_op, list));
            }
             else
                return (print(list));
}
